<?php
require('../database/db.php');
$mysql=new mysql();
error_reporting(0);
// 分页
if($_REQUEST['page']!="" && $_REQUEST['pagesize']!=""){
	$de=$_REQUEST['page']*$_REQUEST['pagesize'];
	$size=$_REQUEST['pagesize'];
}else{
	$de=0;
	$size=10;
}
// 分页 end
$remen_shichang_id=$_REQUEST[remen_shichang_id];
$cateid=$_REQUEST[cateid];
$cityid=$_REQUEST[cityid];
// 传入经纬度
$longitude=$_REQUEST[longitude];
$latitude=$_REQUEST[latitude];
// 传入经纬度 end
// 一个月的时间
$t = getdate(time());
$sdate = $t[year].'-'.$t[mon].'-'.'01 00:00:00';
$edate = $t[year].'-'.$t[mon].'-'.'31 23:59:59';
// 一个月的时间 end
if (($remen_shichang_id!='' || $cateid!='') && $cityid!='' && $longitude!='' && $latitude!='') {
	if ($cateid!='') {
		$sql="select *,(SQRT(POWER(MOD(ABS(longitude - {$longitude}),360),2) + POWER(ABS(latitude - {$latitude}),2))*160) AS distance from shop where city=".$cityid." and leimu=".$cateid." and `open`=1 and `check`=1 ORDER BY distance limit $de,$size";
	}elseif ($remen_shichang_id!='') {
		$sql = "select cateid from remen_shichang_fenl_tu where remen_shichang_id=".$remen_shichang_id;
		$cateidRet=$mysql->getAll($sql);
		$cateidRetLenght=count($cateidRet);
		$cateidArray = array(); 
		for($i=0;$i<$cateidRetLenght;$i++){
			$cateidArray[] = $cateidRet[$i][cateid];
		}
		$cateids = implode(",",$cateidArray);
		if($cateids!=""){
		   $sql="select *,(SQRT(POWER(MOD(ABS(longitude - {$longitude}),360),2) + POWER(ABS(latitude - {$latitude}),2))*160) AS distance from shop where city=".$cityid." and leimu in (".$cateids.") and `open`=1 and `check`=1 ORDER BY distance limit $de,$size";	
		}else{
			$arr = array('code' =>400, 'msg' =>'没有厂商');
			echo json_encode($arr);
			exit();
		}
		
	}
	
	$shop=$mysql->getAll($sql);
	$id=count($shop);
	for($i=0;$i<$id;$i++){
		$shopid=$shop[$i][shopid];
		$sql2= "select count(*) as num from goodsorders where addtime>='$sdate' and addtime<='$edate' and shopid=$shopid and state<>0 and state<>4";
		$result=$mysql->getAll($sql2);
		$shop[$i][number] = $result[0][num];
	}

	$arr = array('code' => 200, 'msg' =>'查询成功' ,'data' =>$shop);
}else{
	$arr = array('code' => 300, 'msg' =>'参数传入有误,需传入分类id=>cateid,热门市场id=>remen_shichang_id(cateid,remen_shichang_id),区域id=>cityid,经度=>longitude,纬度=>latitude');
}
echo json_encode($arr);

?>